﻿<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl"/>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
</head>
<body>

<div class="container-fluid " style="margin-top: 20px">
    <#include "../common/top.ftl"/>

    <div class="row">
        <div class="col-lg-2">
            <#assign menu="formalCustomer"/>
        <#--左侧菜单-->
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-lg-10">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">添加正式客户</h1>
                </div>
            </div>
            <#-- 高级查询 -->
            <form class="form-inline" id="searchForm" action="/customer/list.do?source=1" method="post" style="margin-bottom: 10px">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <input type="hidden" name="pageSize" id="pageSize" value="20">

                <a id="export" href="/formalCustomer/list.do" class="btn btn-info">
                    <span class="glyphicon glyphicon-arrow-left"></span> 返回
                </a>&emsp;
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword"
                           value="${(qo.keyword)!}" placeholder="请输入姓名/电话">
                </div>
                <div class="form-group">
                    <label for="dept">年份:</label>
                    <select class="form-control" id="year" name="year">
                        <option value="-1">全部</option>
                        <#list currentYear..2019 as entity>
                            <option value="${(entity?c)!}">${(entity?c)!}年</option>
                        </#list>
                    </select>
                <#--回显年份-->
                    <script>
                        $("#year").val(${(qo.year?c)!});
                    </script>
                </div>
                <div class="form-group">
                    <label for="dept">月份:</label>
                    <select class="form-control" id="month" name="month">
                        <option value="-1">全部</option>
                        <#list 1..12 as entity>
                            <option name="${entity!}" value="${(entity)!}">${(entity)!}月</option>
                        </#list>
                    </select>
                <#--回显月份-->
                    <script>

                        $(function () {
                            //月份合理化处理，删除未到达时间的月份
                            var options = $("#month option");//保存所有月份 1-12
                            var currentYear = "${currentYear?c!}";
                            var currentMonth = "${currentMonth?c!}";
                            //页面加载完毕执行一次
                            removeMonthByYear();
                            $("#year").change(function () {
                                //值改变后执行一次
                                removeMonthByYear();
                            });
                            //页面加载完，如果当前选中的年份为全部或当前年，则删除相应月份
                            function removeMonthByYear() {
                                var selectedYear = $("#year").val();
                                if(selectedYear == -1){
                                    removeMonthOption(1);
                                }else if(selectedYear == currentYear){
                                    //先添加所有然后在删除
                                    $("#month").append(options);
                                    var m = parseInt(currentMonth) + 1;
                                    removeMonthOption(m);
                                }else{
                                    //删除素有option然后再添加回来
                                    $("#month option").remove();
                                    $("#month").append(options);
                                    $("#month").val(-1);
                                }
                            }
                            //删除指定范围的月份
                            function removeMonthOption(month) {
                                for(var i = month; i <= 12; i++){
                                    $("#month option[name='" + i + "']").remove();
                                }
                                $("#month").val(-1);
                            }
                            $("#month").val(${(qo.month?c)!});
                        });
                    </script>
                </div>

                <div class="form-group">
                    <label for="dept">状态:</label>
                    <select class="form-control" id="status" name="cusStatus">
                        <option value="-1">全部</option>
                        <option value="0">潜在客户</option>
                        <option value="1">开发失败</option>
                        <option value="2">重点关注</option>
                    </select>
                <#--回显客户状态-->
                    <script>
                        $("#status").val(${(cusStatus)!});
                    </script>
                </div>
                <button type="submit" class="btn btn-primary">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>

            </form>

            <table class="table table-striped table-hover" >
                <tr>
                    <th>编号</th>
                    <th>时间</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>电话</th>
                    <th>家庭职业</th>
                    <th>意向课程</th>
                    <th>来源</th>
                    <th>营销人员</th>
                    <th>状态</th>
                    <th>备注</th>
                    <th>操作</th>
                </tr>
            <#-- Freemark指令做循环迭代 -->
                <#list ro.list as entity>
                    <tr>
                        <td>${(entity_index + 1)!}</td>
                        <td>${(entity.inputTime?string("yyyy/MM/dd"))!}</td>
                        <td>${(entity.name)!}</td>
                        <td>${(entity.genderName)!}</td>
                        <td>${(entity.age)!}</td>
                        <td>${(entity.tel)!}</td>
                        <td>${(entity.job)!}</td>
                        <td>${(entity.curriculum.title)!}</td>
                        <td>${(entity.source.title)!}</td>
                        <td>${(entity.seller.name)!}</td>
                        <td>${(entity.statusName)!}</td>
                        <td>${(entity.remark)!}</td>
                        <td>
                            <a role="button" class="btn btn-success btn-xs add_Btn" data-json='${(entity.json)!}'>
                                <span class="glyphicon glyphicon-plus"></span> 添加
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>

        <#--分页-->
            <#include "../common/page.ftl" />
        </div>
    <#--ajax导出数据的iframe-->
        <iframe name="exportTarget" id="exportTarget" style="display:none;"></iframe>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">添加为正式客户</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/formalCustomer/save.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <input type="hidden" name="customer.id">
                    <input type="hidden" name="coursesSurplus">
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">客户：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" readonly name="customer.name"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">课程：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="curriculum.id">
                                <#list (curriculums)! as entity>
                                    <option value="${(entity.id)!}">${(entity.title)!}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">总课程数：</label>
                        <div class="col-lg-6">
                            <input type="number" class="form-control" name="coursesCount" min="0" placeholder="请输入本课程的课程数"/>
                        </div>
                    </div>

                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary btn-submit">提交</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        //添加按钮弹出模态框
        $(".add_Btn").click(function () {
            //清除表单
            //回显表单
            var json = $(this).data("json");
            $("#editForm input[name='customer.name']").val(json.name);
            $("#editForm input[name='customer.id']").val(json.id);
            $("#editForm select[name='curriculum.id']").val(json.curriculumId);
            $("#addModal").modal("show");

        });
        //提交模态框表单
        $(".btn-submit").click(function () {
            //新添加正式客户，把总课程数设置到剩余课程数，保持一致
            var count = $("#editForm input[name='coursesCount']").val();
            $("#editForm input[name='coursesSurplus']").val(count);
            $("#editForm").ajaxSubmit(function (data) {
                if(data.success){
                    $.messager.alert("添加成功！","3秒后返回页面");
                    setTimeout(function () {
                        window.location.href = "/formalCustomer/list.do";
                    },3000)
                }else{
                    msg(data.msg);
                }
            });
        });


    });


</script>

</body>
</html>